package com.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.util.ResultMsg;
import com.travel.entity.TbCart;
import com.travel.mapper.CartMapper;
import com.travel.service.CartService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<CartMapper,TbCart> implements CartService {

    /**
     * 查询所有汽车班次
     * */
    /*@Override
    public ResultMsg getAllCart() {
        List<TbCart> list = super.list();
        if(list==null){
            return  ResultMsg.error(555,"当前没有汽车班次");
        }
        return ResultMsg.success(list);
    }*/
    /**
     * 查询所有车票
     * */
    @Override
    public ResultMsg findAllCart(int current,int size) {
        Page<TbCart> ipage = new Page<>(current,size);
        Page<TbCart> page = super.page(ipage);
        return ResultMsg.success(page);
    }
    /**
     * 通过id查询车票
     * */
    @Override
    public ResultMsg getCartById(int carId) {
        TbCart byId = super.getById(carId);
        if(byId==null){
            return  ResultMsg.error(555,"没有当前的汽车班次");
        }else {
            return ResultMsg.success(byId);
        }
    }
    /**
     * 通过起始站，终点站查询符合条件的汽车班次
     * */
    @Override
    public ResultMsg getCartByName(String carSname, String carEname) {
        QueryWrapper<TbCart> wrapper = new QueryWrapper<>();
        if (carSname!=null&&carEname!=null){
            wrapper.like("car_sname",carSname);
            wrapper.like("car_ename",carEname);
        }else if (carEname==null){
            wrapper.like("car_sname",carSname);
        }else{
            wrapper.like("car_ename",carEname);
        }
        List<TbCart> cartList = super.list(wrapper);
        return ResultMsg.success(cartList);
    }
    /**
     * 添加车票
     * */
    @Override
    public ResultMsg addCart(TbCart tbCart) {
        boolean b = super.saveOrUpdate(tbCart);
        if (tbCart!=null)
            return ResultMsg.success(b);
        else
            return ResultMsg.error(555,"添加汽车票失败");
    }
    /**
     * 删除车票
     * */
    @Override
    public ResultMsg deleteCart(int id) {
        boolean b = super.removeById(id);
        if (b)
            return ResultMsg.success(b);
        return ResultMsg.error(555,"删除失败");

    }
    /**
     * 修改车票
     * */
    @Override
    public ResultMsg updateCart(TbCart tbCart) {
        boolean b = super.saveOrUpdate(tbCart);
        if (tbCart!=null)
            return ResultMsg.success(b);
        else
            return ResultMsg.error(555,"修改汽车票失败");
    }
    /**
     * 通过日期查询汽车班次
     * */
    /*@Override
    public ResultMsg getCartByDate(Date carStime) {
        QueryWrapper<TbCart> wrapper = new QueryWrapper<>();
        wrapper.like("car_stime",carStime);
        List<TbCart> cartList = super.list(wrapper);
        return ResultMsg.success(cartList);
    }*/


}
